﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO;

namespace ProjectQuaDep.Controls
{
    public partial class PaddingNews : System.Web.UI.UserControl
    {
        public int PageSize;
        public int Size;
        public int PageIndex = 1;
        public int SumPage;


        const string BuildLink = "<a {0} href=\"{1}{2}.htm\">{3}{4}</a>&nbsp;";
        //public string BuildLink ;
        string strTrang = "trang-";


        public string LinkSite;

        private string currentPage = "1";
        private string addActive = " class=\"active\"";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Padding(int pageIndex)
        {
            //if (Request.QueryString["NewsRef"] != null && Request.QueryString["NewsRef"].ToString() != "")
            //{
            //    if (StringProcess.CheckInt(Request.QueryString["NewsRef"].ToString()))
            //        currentPage = Request.QueryString["NewsRef"].ToString();
            //    else
            //        currentPage = "1";
            //}
            currentPage = pageIndex.ToString();
            string Text = "";
            int iCurrentPage = 0;
            if (Utils.IsNumber(currentPage))
                int.TryParse(currentPage, out iCurrentPage);
            else
                iCurrentPage = 1;
            if (iCurrentPage == 0) iCurrentPage = 1;
            ltrImageList.Text = "";

            //SumPage = (Size - 1) / PageSize + 1;

            string currentBegin = "";
            string currentEnd = "";
            if (SumPage <= 4)
            {
                if (SumPage != 1)
                {
                    for (int i = 1; i <= SumPage; i++)
                    {
                        currentBegin = currentPage == i.ToString() ? addActive : "";
                        currentEnd = currentPage == i.ToString() ? "" : "";
                        Text += String.Format(BuildLink, currentBegin, LinkSite, strTrang + i, i, currentEnd, "");
                    }
                }
            }
            else
            {
                if (iCurrentPage > 1)
                {
                    //refs[1] = 1.ToString();
                    Text += String.Format(BuildLink, "", LinkSite.TrimEnd('/'), "", "Trang đầu", "");
                }
                else
                {
                    for (int i = 1; i <= 3; i++)
                    {
                        currentBegin = currentPage == i.ToString() ? addActive : "";
                        currentEnd = currentPage == i.ToString() ? "" : "";
                        //refs[1] = i.ToString();
                        Text += String.Format(BuildLink, currentBegin, LinkSite, strTrang + i.ToString(), i, currentEnd);
                    }
                    //refs[1] = "4";
                    Text += String.Format(BuildLink, "", LinkSite, strTrang + "4", "...", "");
                }
                if (iCurrentPage > 1 && iCurrentPage < SumPage)
                {
                    if (iCurrentPage > 2)
                    {
                        //refs[1] = (iCurrentPage - 2).ToString();
                        Text += String.Format(BuildLink, "", LinkSite, strTrang + (iCurrentPage - 2).ToString(), "...", "");
                    }
                    for (int i = (iCurrentPage - 1); i <= (iCurrentPage + 1); i++)
                    {
                        //refs[1] = i.ToString();
                        currentBegin = currentPage == i.ToString() ? addActive : "";
                        currentEnd = currentPage == i.ToString() ? "" : "";
                        Text += String.Format(BuildLink, currentBegin, LinkSite, strTrang + i.ToString(), i, currentEnd);
                    }
                    if (iCurrentPage <= SumPage - 2)
                    {
                        //refs[1] = (iCurrentPage + 2).ToString();
                        Text += String.Format(BuildLink, "", LinkSite, strTrang + (iCurrentPage + 2).ToString(), "...", "");
                    }
                }
                int intCurrentPage = 0;
                int.TryParse(currentPage, out intCurrentPage);
                if (intCurrentPage == 0) intCurrentPage = 1;
                if (intCurrentPage < SumPage)
                {
                    //refs[1] = SumPage.ToString();
                    Text += String.Format(BuildLink, "", LinkSite, strTrang + SumPage.ToString(), "Trang cuối", "");
                }
                else
                {
                    //refs[1] = (SumPage - 4).ToString();
                    Text += String.Format(BuildLink, "", LinkSite, strTrang + (SumPage - 4).ToString(), "...", "");
                    int j = 3;
                    for (int i = SumPage; i >= SumPage - 3; i--)
                    {
                        //refs[1] = (SumPage - j).ToString();                        
                        currentBegin = currentPage == (SumPage - j).ToString() ? addActive : "";
                        currentEnd = currentPage == (SumPage - j).ToString() ? "" : "";

                        Text += String.Format(BuildLink, currentBegin, LinkSite, strTrang + (SumPage - j).ToString(), SumPage - j, currentEnd);
                        j--;
                    }

                }
            }
            ltrImageList.Text = Text;
        }
    }
}